﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class RegimenPensionarioRepository: MasterRepository,IRegimenPensionarioRepository
    {
        public List<REGIMEN_PENSIONARIO> GetAllFromRegimenPensionario()
        {
            var query = from p in context.REGIMEN_PENSIONARIO
                        select p;

            return query.ToList();
        }

        public List<REGIMEN_PENSIONARIO> GetAllFromByDescripcion(string descripcion)
        {
            if (descripcion != string.Empty)
            {
                var query = from p in context.REGIMEN_PENSIONARIO
                            where p.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select p;

                return query.ToList();

            }
            else
            {
                var query = from p in context.REGIMEN_PENSIONARIO
                            select p;

                return query.ToList();
            }
        }

        public REGIMEN_PENSIONARIO GetFromRegimenPensionarioById(int id)
        {
            var query = from p in context.REGIMEN_PENSIONARIO
                        where p.IdRegimenPensionario.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public REGIMEN_PENSIONARIO GetFromRegimenPensionarioByCodigo(string codigo)
        {
            var query = from ds in context.REGIMEN_PENSIONARIO
                        where ds.Codigo.Equals(codigo)
                        select ds;
            return query.SingleOrDefault();
        }

        public void AddRegimenPensionario(REGIMEN_PENSIONARIO regPensionario)
        {
            context.REGIMEN_PENSIONARIO.Add(regPensionario);
            context.SaveChanges();
        }

        public void UpdateRegimenPensionario(REGIMEN_PENSIONARIO regPensionario)
        {
            context.Entry(regPensionario).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteRegimenPensionario(int id)
        {
            var existe = context.REGIMEN_PENSIONARIO.Where(p => p.IdRegimenPensionario.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.REGIMEN_PENSIONARIO.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion,string codigo, int id)
        {
            var query = from p in context.REGIMEN_PENSIONARIO
                        where (p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 || p.Codigo.ToUpper().CompareTo(codigo.ToUpper()) == 0)
                        && p.IdRegimenPensionario != id
                        select p;

            return query.Any();
        }
    }
}
